/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.DocProject
{
	public sealed class DataGridViewFileColumn : DataGridViewColumn
	{
		#region Public Properties
		public override DataGridViewCell CellTemplate
		{
			get
			{
				return base.CellTemplate;
			}
			set
			{
				if (value != null && !value.GetType().IsAssignableFrom(typeof(DataGridViewFileCell)))
					throw new InvalidCastException(Resources.Errors.DataGridViewFileCellExpected);

				base.CellTemplate = value;
			}
		}

		public bool CanSelectFolders
		{
			get
			{
				return canSelectFolders;
			}
			set
			{
				canSelectFolders = value;
			}
		}

		public bool CanSelectFiles
		{
			get
			{
				return canSelectFiles;
			}
			set
			{
				canSelectFiles = value;
			}
		}
		#endregion

		#region Private / Protected
		private bool canSelectFolders = true, canSelectFiles = true;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DataGridViewFileColumn" /> class.
		/// </summary>
		public DataGridViewFileColumn()
			: base(new DataGridViewFileCell())
		{
		}
		#endregion

		#region Methods
		public override string ToString()
		{
			StringBuilder builder = new StringBuilder(64);
			builder.Append("DataGridViewFileColumn { Name=");
			builder.Append(base.Name);
			builder.Append(", Index=");
			builder.Append(base.Index.ToString(System.Globalization.CultureInfo.CurrentCulture));
			builder.Append(" }");
			return builder.ToString();
		}
		#endregion

		#region Events
		private readonly object InitializeFileDialogEventLock = new object();
		private EventHandler<DataGridViewFileColumnDialogEventArgs> InitializeFileDialogEvent;
		private readonly object InitializeFolderDialogEventLock = new object();
		private EventHandler<DataGridViewFileColumnFolderDialogEventArgs> InitializeFolderDialogEvent;

		/// <summary>
		/// Event raised when the <see cref="FolderBrowserDialog"/> is initialized for the current cell in edit mode.
		/// </summary>
		public event EventHandler<DataGridViewFileColumnFolderDialogEventArgs> InitializeFolderDialog
		{
			add
			{
				lock (InitializeFolderDialogEventLock)
				{
					InitializeFolderDialogEvent += value;
				}
			}
			remove
			{
				lock (InitializeFolderDialogEventLock)
				{
					InitializeFolderDialogEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="InitializeFolderDialog" /> event.
		/// </summary>
		/// <param name="e"><see cref="DataGridViewFileColumnDialogEventArgs" /> object that provides the arguments for the event.</param>
		internal void OnInitializeFolderDialog(DataGridViewFileColumnFolderDialogEventArgs e)
		{
			EventHandler<DataGridViewFileColumnFolderDialogEventArgs> handler = null;

			lock (InitializeFolderDialogEventLock)
			{
				handler = InitializeFolderDialogEvent;
			}

			if (handler != null)
				handler(this, e);
		}

		/// <summary>
		/// Event raised when the <see cref="OpenFileDialog"/> is initialized for the current cell in edit mode.
		/// </summary>
		public event EventHandler<DataGridViewFileColumnDialogEventArgs> InitializeFileDialog
		{
			add
			{
				lock (InitializeFileDialogEventLock)
				{
					InitializeFileDialogEvent += value;
				}
			}
			remove
			{
				lock (InitializeFileDialogEventLock)
				{
					InitializeFileDialogEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="InitializeFileDialog" /> event.
		/// </summary>
		/// <param name="e"><see cref="OpenFileDialogEventArgs" /> object that provides the arguments for the event.</param>
		internal void OnInitializeFileDialog(DataGridViewFileColumnDialogEventArgs e)
		{
			EventHandler<DataGridViewFileColumnDialogEventArgs> handler = null;

			lock (InitializeFileDialogEventLock)
			{
				handler = InitializeFileDialogEvent;
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion
	}
}
